#ifndef U_PEER_MANAGER_H
#define U_PEER_MANAGER_H

#include "i_package_receiver.h"
#include "i_package_sender.h"
#include "i_connection_listener.h"
#include "i_io_handler.h"
#include "singleton.h"
#include "register_surpport.h"
#include "u_peer.h"
#include "u_service.h"

#include <boost/asio/ip/udp.hpp>
using boost::asio::ip::udp;

class UPeerManager : public IPackageReceiver, public ISingleton<UPeerManager>, public IRegister<udp::endpoint, UPeer>
{
	IConnectionListener::Ptr connectionListener_;
	void handleNewPeerMessage(const udp::endpoint& remoteAddr, IPackage::Ptr package, IPackageSender::Ptr packageHandler, unsigned int packageSeq);
public:
	virtual void handle(const udp::endpoint& remoteAddr, IPackage::Ptr package, 
		IPackageSender::Ptr packageHandler, unsigned int packageSeq);

	void onConnectionClosed(const udp::endpoint& remoteAddr, UPeer::ShutdownReason reason);
	void onConnectionEstablished(UPeer::Ptr peer, bool connectingSide);

	UPeer::Ptr createChannelTo(const udp::endpoint& remoteAddr, IPackageSender::Ptr packageHandler);

	void setConnectionListener(IConnectionListener::Ptr listener);
};

#endif
